package com.hspedu.api_;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * @author DL5O
 * @version 1.0
 * 演示INetAddress类的使用
 */
public class API_ {
    public static void main(String[] args) throws UnknownHostException {
        //1.获取本机的InetAddress对象
        InetAddress localHost = InetAddress.getLocalHost();
        System.out.println(localHost);
        //可以获取主机名和主机ip地址

        //2.根据指定的主句名，获取InetAddress
        InetAddress byName = InetAddress.getByName("DESKTOP-5MC7HC7");
        System.out.println("host1=" + byName);

        //3.根据域名返回 InetAddress对象，比如www.baidu.com 对应的InetAddress对象
        InetAddress host2 = InetAddress.getByName("www.baidu.com");
        System.out.println("host2=" + host2);//www.baidu.com/110.242.68.3

        //4.通过InetAddress对象，获取地址
        String hostAddress = host2.getHostAddress();
        System.out.println("hostAddress=" + hostAddress);//110.242.68.4

        //5.通过InetAddress对象，获取对应的主机名或者是域名
        String hostName = host2.getHostName();
        System.out.println("hostName=" + hostName);//www.baidu.com

    }
}

